package com.alaeat.util;

import com.alaeat.entity.ReportData;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author cuishidong
 */
public class Utils {
    public static void appendDataToExcel(String outputFilePath, String propertyId, String title, String[] header, List<ReportData> newData) throws IOException {
        Workbook workbook;
        File file = new File(outputFilePath);
        if (file.exists()) {
            FileInputStream inputStream = new FileInputStream(file);
            workbook = new XSSFWorkbook(inputStream);
            inputStream.close();
        } else {
            workbook = new XSSFWorkbook();
        }
        Sheet sheet = workbook.getSheet("Sheet1");
        if (sheet == null) {
            // 如果工作簿中不存在指定名称的工作表，则新建一个工作表
            sheet = workbook.createSheet("Sheet1");
        }

        // 设置标题样式
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        Font titleFont = workbook.createFont();
        titleFont.setBold(true);
        titleFont.setFontHeightInPoints((short) 14);
        titleStyle.setFont(titleFont);
        int lastRowNum = sheet.getLastRowNum();
        if (lastRowNum > 0) {
            lastRowNum = lastRowNum + 2;
        }else {
            lastRowNum = 0;
        }
        // 创建标题行
        Row titleRow = sheet.createRow(lastRowNum);
        Cell titleCell = titleRow.createCell(0);
        Cell titleCell1 = titleRow.createCell(1);
        titleCell.setCellValue(propertyId);
        titleCell.setCellStyle(titleStyle);
        titleCell1.setCellValue(title);
        titleCell1.setCellStyle(titleStyle);
        lastRowNum = sheet.getLastRowNum();
        // 创建表头行
        Row headerRow = sheet.createRow(lastRowNum + 1);
        for (int i = 0; i < header.length; i++) {
            Cell headerCell = headerRow.createCell(i);
            headerCell.setCellValue(header[i]);
            headerCell.setCellStyle(getHeaderCellStyle(workbook));
        }
        // 创建新行，并添加新数据
        for (int i = 0; i < newData.size(); i++) {
            lastRowNum = sheet.getLastRowNum();
            Row newRow = sheet.createRow(lastRowNum + 1);
            ReportData reportData = newData.get(i);
            newRow.createCell(0).setCellValue(reportData.getDimension());
            newRow.createCell(1).setCellValue(reportData.getActiveUsers());
            newRow.createCell(2).setCellValue(reportData.getSessions());
            newRow.createCell(3).setCellValue(reportData.getEngagedSessions());
            newRow.createCell(4).setCellValue(reportData.getAverageSessionDuration());
            newRow.createCell(5).setCellValue(reportData.getSessionsPerUser());
            newRow.createCell(6).setCellValue(reportData.getEventsPerSession());
            newRow.createCell(7).setCellValue(reportData.getEngagementRate());
            newRow.createCell(8).setCellValue(reportData.getEventCount());
            newRow.createCell(9).setCellValue(reportData.getConversions());
            newRow.createCell(10).setCellValue(reportData.getTotalRevenue());
        }

        // 写入到输出文件
        FileOutputStream outputStream = new FileOutputStream(outputFilePath);
        workbook.write(outputStream);
        outputStream.close();
    }
    public static CellStyle getHeaderCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        return style;
    }
    public static CellStyle getDataCellStyle(Workbook workbook, int index) {
        CellStyle style = workbook.createCellStyle();
        if (index % 2 == 0) {
            style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        } else {
            style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
        }
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        return style;
    }
}
